part of "../version.dart";

///MajorUpdate
class MajorUpdate implements Jsonable, Comparable<MajorUpdate> {
  ///目的，这里主要介绍为什么切换了一个大版本，整个大版本的目的，更多是战略层面的思考，而非每次干活更新的内容，它可以独立于每次更新而存在，只要有一个对未来的想象了就可以推出
  String intention;

  /// 版本号，其中修订版必然为-1
  final Version version;

  ///所有修订更新，当前大版本下的所有修订版更新
  final List<PatchUpdate> patchUpdates;

  ///主题，通常是1-4个中文字，就好像是本子的款式名称一样，用来概括本次较大更新的主题
  String subject;

  MajorUpdate({
    required this.intention,
    required this.patchUpdates,
    required this.subject,
    required this.version,
  });

  factory MajorUpdate.fromRaw(Json raw) {
    final int major = raw["major"];
    final int minor = raw["minor"];
    return MajorUpdate(
      intention: raw["intention"],
      version: Version(major, minor, -1),
      patchUpdates: (raw["patchUpdates"] as JsonList)
          .unzip((raw) => PatchUpdate.fromRaw(raw, major, minor)),
      subject: raw["subject"],
    );
  }

  /// 比如给前端用于数据还没有加载时提供的填充用的空的更新版本
  factory MajorUpdate.empty() => MajorUpdate(
        intention: "加载中",
        version: Version(0, 0, 0),
        patchUpdates: [
          PatchUpdate(
            intention: "加载中",
            patches: [],
            updates: [],
            version: Version(0, 0, 0),
          ),
        ],
        subject: "加载中",
      );

  @override
  Json toJson() => {
        "intention": intention,
        "major": version.major,
        "minor": version.minor,
        "subject": subject,
        "patchUpdates": patchUpdates.toJson(),
      };

  /// 排序的依据为版本号的大小
  @override
  int compareTo(MajorUpdate other) => version.compareTo(other.version);
}
